/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
import java.math.BigDecimal;
import java.util.*;
import java.sql.Timestamp;
import org.ofbiz.entity.*;
import org.ofbiz.entity.condition.*;
import org.ofbiz.entity.util.*;
import org.ofbiz.base.util.*;
import org.ofbiz.base.util.collections.*;
import org.ofbiz.order.order.*;
import org.ofbiz.party.contact.*;
import org.ofbiz.product.inventory.InventoryWorker;
import org.ofbiz.product.catalog.CatalogWorker;
import org.ofbiz.accounting.payment.*;

import javolution.util.FastMap;

orderId = table.orderId;
orderHeader = null;
orderItems = null;
orderAdjustments = null;
orderHeader = null;
orderReadHelper = null;
orderItems = null;
orderItemSize = null;
orderAdjustments = null;
orderHeaderAdjustments = null;
orderSubTotal = null;
currencyUomId = null;
orderTerms = null;
taxTotal = null;

if (orderId) {
    orderHeader = delegator.findOne("OrderHeader", [orderId : orderId], false);
}

if (orderHeader) {

    orderReadHelper = new OrderReadHelper(orderHeader);
    taxTotal = orderReadHelper.getTaxTotal();
    orderItems = orderReadHelper.getOrderItems();
    orderItems = EntityUtil.filterByCondition(orderItems, EntityCondition.makeCondition("statusId", EntityOperator.IN, ["ITEM_APPROVED", "ITEM_COMPLETED"]));
    orderAdjustments = orderReadHelper.getAdjustments();
    orderHeaderAdjustments = orderReadHelper.getOrderHeaderAdjustments();
    orderSubTotal = orderReadHelper.getOrderItemsSubTotal();
    orderTerms = orderHeader.getRelated("OrderTerm", null, null, false);
    orderItemSize = orderItems.size();
    currencyUomId = orderReadHelper.getCurrency();
}
context.orderHeader = orderHeader;
context.orderReadHelper = orderReadHelper;
context.orderItems = orderItems;
context.orderItemSize = orderItemSize;
context.orderAdjustments = orderAdjustments;
context.orderHeaderAdjustments = orderHeaderAdjustments;
context.orderSubTotal = orderSubTotal;
context.currencyUomId = currencyUomId;
context.orderTerms = orderTerms;
context.taxTotal = taxTotal;
// VAT
vatString = null;
vatList = delegator.findByAnd("TaxAuthorityRateProduct", UtilMisc.toMap("taxAuthPartyId", "RESTAURANT_TAX", "taxAuthorityRateTypeId", "VAT_TAX"), null, false);
vatList = EntityUtil.filterByDate(vatList);
if (vatList.size() > 0) {
    vat = vatList.get(0).getBigDecimal("taxPercentage");
    vatString = "VAT " +  (vat.setScale(0)) + " %";
}
context.vatString = vatString;
// Invoice
hasInvoice = false;
orderItemBillingList = delegator.findByAnd("OrderItemBilling", UtilMisc.toMap("orderId", orderId), null, false);
if (orderItemBillingList.size() > 0) {
    hasInvoice = true;
}
context.hasInvoice = hasInvoice;